package com.woniu108.evaluate.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu108.carService.commos.dto.UserInfoDto;
import com.woniu108.carService.commos.exception.WoniuException;
import com.woniu108.carService.commos.exception.evaluate.EvaluateException;
import com.woniu108.carService.commos.exception.evaluate.EvaluateExceptionCode;
import com.woniu108.carService.commos.exception.orderProduct.OrderProductException;
import com.woniu108.carService.commos.exception.orderProduct.OrderProductExceptionCode;
import com.woniu108.carService.commos.model.Result;
import com.woniu108.carService.commos.util.StaticData;
import com.woniu108.evaluate.client.OrderClient;
import com.woniu108.evaluate.client.PersonClient;
import com.woniu108.evaluate.dto.StoreReviewDto;
import com.woniu108.evaluate.model.StoreReview;
import com.woniu108.evaluate.mapper.StoreReviewMapper;
import com.woniu108.evaluate.param.StoreReviewPublishParam;
import com.woniu108.evaluate.param.StoreReviewReplyParam;
import com.woniu108.evaluate.service.StoreReviewService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu108.order.model.dto.OrderServiceDto;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 门店评价表 服务实现类
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@Service
public class StoreReviewServiceImpl extends ServiceImpl<StoreReviewMapper, StoreReview> implements StoreReviewService {
    @Resource
    private OrderClient orderClient;
    @Resource
    private PersonClient personClient;
    @Resource
    private StoreReviewMapper storeReviewMapper;
    @Override
    @Transactional(rollbackFor = Throwable.class)
    public Boolean publishReview(StoreReviewPublishParam param) {
        //查询订单状况
        Result<OrderServiceDto> orderServiceDtoResult = orderClient.queryOrderServiceByOrderServiceNum(param.getOrderServiceNum());
        if(orderServiceDtoResult.getCode()!=200){
            throw new WoniuException(orderServiceDtoResult.getMsg(),orderServiceDtoResult.getCode());
        }
        OrderServiceDto orderServiceDto = orderServiceDtoResult.getData();
        //订单是否存在或删除
        if(orderServiceDto==null||orderServiceDto.getOrderServiceState().equals(StaticData.ORDER_SERVICE_STATE_DELETED)){
            throw new OrderProductException(OrderProductExceptionCode.ORDER_NULL);
        }
        //用户是否能操作
        if(!orderServiceDto.getUserId().equals(param.getUserId())){
            throw new OrderProductException(OrderProductExceptionCode.ORDER_PRODUCT_USER_NOT_POWER);
        }
        //查询
        QueryWrapper<StoreReview> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_service_num",param.getOrderServiceNum());
        queryWrapper.eq("store_id",orderServiceDto.getStoreId());
        queryWrapper.eq("spoke_id",param.getUserId());
        StoreReview storeReview = storeReviewMapper.selectOne(queryWrapper);
        if(storeReview!=null){
            throw new EvaluateException(EvaluateExceptionCode.EVALUATE_ALREADY_REVIEWED);
        }
        storeReview=new StoreReview();
        storeReview.setSpokeId(param.getUserId());
        storeReview.setStoreId(orderServiceDto.getStoreId());
        storeReview.setReview(param.getReview());
        storeReview.setOrderServiceNum(param.getOrderServiceNum());
        storeReview.setReplyReId(0);
        storeReviewMapper.insert(storeReview);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public Boolean replyByReview(StoreReviewReplyParam param) {
        StoreReview storeReview = storeReviewMapper.selectById(param.getProReviewId());
        if(storeReview==null){
            throw new EvaluateException(EvaluateExceptionCode.EVALUATE_REVIEW_NO_EXISTED);
        }
        StoreReview replyReview = storeReviewMapper.selectOne(new QueryWrapper<StoreReview>().eq("reply_re_id", param.getProReviewId()));
        if(replyReview!=null){
            throw new EvaluateException(EvaluateExceptionCode.EVALUATE_REVIEW_REPLY_EXISTED);
        }
        replyReview=new StoreReview();
        replyReview.setReplyId(param.getStoreAdminId());
        replyReview.setReview(param.getReview());
        replyReview.setReplyReId(param.getProReviewId());
        storeReviewMapper.insert(replyReview);
        return true;
    }

    @Override
    public List<StoreReviewDto> selectProductReviewByPItemId(Integer storeId) {
        //查询
        QueryWrapper<StoreReview> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("reply_re_id",0);
        //用户商品评价
        List<StoreReview> storeReviews = storeReviewMapper.selectList(queryWrapper);
        System.out.println(storeReviews);
        //预备评价集合
        List<StoreReviewDto> productReviewDtos= new ArrayList<>();
        for(StoreReview storeReview:storeReviews){
            StoreReviewDto reviewDto = BeanUtil.toBean(storeReview, StoreReviewDto.class);
            Result<UserInfoDto> userInfoDtoResult = personClient.getByUserId(reviewDto.getSpokeId());
            if(userInfoDtoResult.getCode()!=200){
                throw new WoniuException(userInfoDtoResult.getMsg(),userInfoDtoResult.getCode());
            }
            UserInfoDto userInfoDto = userInfoDtoResult.getData();
            reviewDto.setSpokeNickname(userInfoDto.getUserNickName());
            StoreReview replyReview = storeReviewMapper.selectOne(new QueryWrapper<StoreReview>().eq("reply_re_id", reviewDto.getProReviewId()));
            if(replyReview!=null){
                reviewDto.setReplyId(replyReview.getReplyId());
                reviewDto.setReplyReview(replyReview.getReview());
            }
            productReviewDtos.add(reviewDto);
        }
        return productReviewDtos;
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public Boolean delReviewByIdToUser(Integer proReviewId, String userId) {
        StoreReview storeReview = storeReviewMapper.selectById(proReviewId);
        if(storeReview==null){
            throw new EvaluateException(EvaluateExceptionCode.EVALUATE_REVIEW_NO_EXISTED);
        }
        if(!storeReview.getSpokeId().equals(userId)){
            throw new EvaluateException(EvaluateExceptionCode.EVALUATE_REVIEW_USER_NOT_POWER);
        }
        storeReviewMapper.deleteById(proReviewId);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public Boolean delReviewById(Integer proReviewId) {
        StoreReview storeReview = storeReviewMapper.selectById(proReviewId);
        if(storeReview==null){
            throw new EvaluateException(EvaluateExceptionCode.EVALUATE_REVIEW_NO_EXISTED);
        }
        storeReviewMapper.deleteById(proReviewId);
        return true;
    }
}
